package com.fy.market.strategy.repository;

import com.baomidou.mybatisplus.extension.service.IService;
import com.fy.market.strategy.model.dto.UserQueryDTO;
import com.fy.market.strategy.model.entity.*;
import com.fy.market.strategy.model.entity.tree.RuleTreeEntity;
import com.fy.market.strategy.model.entity.tree.RuleTreeNodeEntity;
import com.fy.market.strategy.model.entity.tree.RuleTreeNodeLineEntity;
import com.fy.market.strategy.model.entity.tree.valobj.RuleTreeVO;

import java.util.List;
import java.util.Map;

/**
 * 策略仓储接口
 * @author fwq
 * @date 2025-08-11 10:00
 */
public interface IStrategyRepository<T> extends IService<T> {

    /**
     * 获取策略实体
     * @param strategyId
     * @return 策略实体
     */
    StrategyEntity getStrategyEntity(Integer strategyId);

    /**
     * 查询权重总和
     * @param strategyId
     * @param matchedTimes
     * @return 权重总和
     */
    Integer queryAllRate(Integer strategyId, Integer matchedTimes);

    /**
     * 添加策略规则权重到Redis缓存
     * @param strategyId 策略ID
     * @param rateRange 权重范围
     * @param times 权重次数
     * @param rangeIndexToAwardIdMap 权重范围到奖品ID的映射
     */
    void addRuleWeightRedisCache(Integer strategyId, Integer rateRange, Integer times, Map<Integer, Map<Integer, Integer>> rangeIndexToAwardIdMap);

    /**
     * 添加策略规则权重到Redis缓存
     * @param strategyId 策略ID
     * @param rangeIndexToAwardIdMap 权重范围到奖品ID的映射
     */
    void addDefaultRedisCache(Integer strategyId, Map<Integer, Integer> rangeIndexToAwardIdMap);

    /**
     * 添加所有权重总和
     * @param strategyId 策略ID
     * @param allRate 所有权重总和
     */
    void addAllRateCache(Integer strategyId, Integer allRate);

    /**
     * 执行抽奖
     * @param strategyId
     * @param randomIndex
     * @return 奖品ID
     */
    Integer doRaffle(Integer strategyId, Integer matchedTimes, Integer randomIndex);

    /**
     * 获取策略的规则模型实体集合
     * @param strategyId
     * @return
     */
    List<StrategyRuleModelEntity> getRuleModelList(Integer strategyId);

    /**
     * 按抽奖次数分组，返回每个次数对应的奖品ID列表
     */
    Map<Integer, List<Integer>> queryRuleWeightGroupByTimes(Integer strategyId);

    /**
     * 查询黑名单
     * @param ruleMatterEntity
     * @return 黑名单信息
     */
    StrategyRuleBlacklistEntity queryBlackList(RuleMatterEntity ruleMatterEntity);
    /**
     * 查询策略奖品规则列表
     * @param strategyId 策略ID
     * @return 策略奖品规则列表
     */
    List<StrategyRuleEntity> queryStrategyRuleList(Integer strategyId);

    /**
     * 查询策略奖品列表
     * @param strategyId 策略ID
     * @return 策略奖品列表
     */
    List<StrategyAwardRuleEntity> queryStrategyAwardRuleEntities(Integer strategyId);


    RuleTreeVO getRuleTreeVO(RuleMatterEntity ruleMatterEntity);

    /**
     * 扣减奖品库存
     *
     * @param strategyId 策略ID
     * @param awardId    奖品ID
     * @return 扣减结果 [错误码, 剩余库存, 消息]
     */
    DecrAwardStockResultEntity decrAwardStock(Integer strategyId, Integer awardId);

     /**
      * 缓存策略奖品库存
      * @param strategyId 策略ID
      */
    void addStockCache(Integer strategyId);

    /**
     * 订单生产者
     *
     * @param orderMessageEntity 订单消息实体
     */
     void sendOrder(OrderMessageEntity orderMessageEntity);
}
